package cn.hyy.FVOnline.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import cn.hyy.FVOnline.entity.Goods;

import java.util.List;

@Repository("goodsMapper")
public interface GoodsMapper {

    @Results(id = "goodsMap",value = {
            @Result(property = "id",column = "id",id = true),
            @Result(property = "goodsname",column = "goodsname"),
            @Result(property = "hsPath",column = "hspath"),
            @Result(property = "shopid",column = "shopid"),
            @Result(property = "details",column = "details"),
            @Result(property = "type",column = "type"),
            @Result(property = "price",column = "price"),
            @Result(property = "number",column = "number"),
            @Result(property = "sellnum",column = "sellnum"),
            @Result(property = "state",column = "state")
    })
    @Select("select * from goods where shopid = #{shopid} and state = 1 limit #{start}, #{pageSize}")
    List<Goods> findPageGoods(@Param("shopid")Integer shopid,@Param("start") Integer start, @Param("pageSize")Integer pageSize);

    @Select("select * from goods where shopid = #{shopid} and type = #{type} and goodsname like #{goodsname} and state = 1 limit #{start}, #{pageSize}")
    List<Goods> findByTypeName(@Param("shopid")Integer shopid, @Param("start") Integer start, @Param("pageSize")Integer pageSize, @Param("type")String type, @Param("goodsname") String goodsname);

    @Select("select count(*) from goods where shopid = #{shopid} and state = 1")
    Integer getTotal(@Param("shopid")Integer shopid);

    @Select("select count(*) from goods where shopid = #{shopid} and type = #{type} and goodsname like #{goodsname} and state = 1")
    Integer getTotalByTypeName(@Param("shopid")Integer shopid, @Param("type")String type, @Param("goodsname") String goodsname);

    @Delete("delete from goods where id = #{id}")
    void deleteGoods(@Param("id")Integer id);

    @Update("update goods set goodsname = #{goods.goodsname}, details = #{goods.details}, " +
            " type = #{goods.type}, price = #{goods.price}, number = #{goods.number} where id = #{goods.id}")
    void updateGoods(@Param("goods")Goods goods);


    @Insert("insert into goods (goodsname,details,shopid,price,number,type) values (#{goods.goodsname},#{goods.details}," +
            "#{goods.shopid},#{goods.price},#{goods.number},#{goods.type})")
    @Options(useGeneratedKeys = true,keyProperty = "goods.id")
    void newGoods(@Param("goods")Goods goods);

    @Update("update goods set hspath = #{goods.hsPath} where id = #{goods.id}")
    void setPath(@Param("goods")Goods goods);

    @Select("select * from goods where shopid = #{shopid} and goodsname like #{goodsname} and state = 1 limit #{start}, #{pageSize}")
    List<Goods> findByName(@Param("shopid")Integer shopid, @Param("start") Integer start, @Param("pageSize")Integer pageSize, @Param("goodsname") String goodsname);

    @Select("select count(*) from goods where shopid = #{shopid} and goodsname like #{goodsname} and state = 1")
    Integer getTotalByName(@Param("shopid")Integer shopid, @Param("goodsname") String goodsname);


    @Select("select * from goods where goodsname like #{goodsname} and state = 1 limit #{start}, #{pageSize}")
    List<Goods> findGoodsList(@Param("start") Integer start, @Param("pageSize")Integer pageSize, @Param("goodsname") String goodsname);

    @Select("select count(*) from goods where goodsname like #{goodsname} and state = 1")
    Integer getListTotal(@Param("goodsname") String goodsname);

    @Select("select * from goods where id = #{goodsid}")
    Goods findById(@Param("goodsid") Integer goodsid);

    @Update("update goods set state = 2 where id = #{id}")
    void delGoods(@Param("id") Integer id);

    @Select("select * from goods where goodsname like #{goodsname} and state = 0 limit #{start}, #{pageSize}")
    List<Goods> findGoodsList2(@Param("start") Integer start, @Param("pageSize")Integer pageSize, @Param("goodsname") String goodsname);

    @Select("select count(*) from goods where goodsname like #{goodsname} and state = 0")
    Integer getListTotal2(@Param("goodsname") String goodsname);

    @Update("update goods set state = 1 where id = #{id}")
    void addGoods(@Param("id") Integer id);

    @Update("update goods set sellnum = sellnum + #{sell} where id = #{id}")
    void addSell(@Param("sell")Integer sell,@Param("id")Integer id);

}
